/**
 * 
 */
package com.hz.winnie.demands.web.controller.product;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hz.winnie.demands.pojo.Demand;
import com.hz.winnie.demands.pojo.Product;
import com.hz.winnie.demands.service.DemandsService;
import com.hz.winnie.demands.service.ProductService;

/**
 * @description 产品Controller
 * @author winnie
 * @date 2011-4-8
 * @version 1.0
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    private static int limit = 10;

    @Autowired
    private ProductService productService;

    @Autowired
    private DemandsService demandsService;

    /**
     * 新建产品
     * 
     * @param product
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(Product product, Model model) {
	if (product == null || StringUtils.isBlank(product.getName())) {
	    return "product/add";
	}
	if (productService.isSameName(product.getName(), null)) {
	    model.addAttribute("msg", "产品名称重复");
	    return "error";
	}
	int productId = productService.addProduct(product);
	model.addAttribute("productId", productId);
	return "product/success";
    }

    /**
     * 修改产品
     * 
     * @param product
     * @param productId
     * @param model
     * @return
     */
    @RequestMapping("/edit/{productId}")
    public String edit(@ModelAttribute Product product,
	    @PathVariable String productId, Model model) {
	if (product == null || StringUtils.isBlank(product.getName())) {
	    product = productService.getProductById(productId);
	    if (product == null) {
		return "error";
	    }
	    model.addAttribute("product", product);
	    return "product/edit";
	}
	productService.editProduct(product);
	model.addAttribute("productId", productId);
	model.addAttribute("isEdit", true);
	return "product/success";
    }

    /**
     * 产品明细
     * 
     * @param productId
     * @param model
     * @return
     */
    @RequestMapping("/detail/{productId}")
    public String detail(@PathVariable String productId, Model model,
	    @RequestParam(defaultValue = "1") String currentPage) {
	Product product = productService.getProductById(productId);

	if (product == null) {
	    return "page_not_found";
	}

	/** 产品下的所有需求 */
	Demand demand = new Demand();
	demand.setProductId(Integer.parseInt(productId));
	demand.setCurrentPage(Integer.valueOf(currentPage));
	demand.setLimit(limit);
	if (demand.getCurrentPage() == 0) {
	    demand.setCurrentPage(1);
	}
	int start = (demand.getCurrentPage() - 1) * limit;
	demand.setStart(start);
	List<Demand> demands = demandsService.listDemandsByCondition(demand);

	/** 需求个数 */
	int resultCount = demandsService.countDemandsByCondition(demand);

	model.addAttribute("demand", demand);
	model.addAttribute("totalPages", resultCount % limit == 0 ? resultCount
		/ limit : resultCount / limit + 1);
	model.addAttribute("currentPage", demand.getCurrentPage());
	model.addAttribute("product", product);
	model.addAttribute("demands", demands);
	model.addAttribute("resultCount", resultCount);
	return "product/detail";
    }

    /**
     * 是否有相同名字的产品，如果传递productId表示是修改的时候，要出去这个产品本身
     * 
     * @param name
     * @param productId
     * @return
     */
    @RequestMapping("/isSameName.htm")
    @ResponseBody
    public String isSameName(String name, String productId) {
	if (productService.isSameName(name, productId)) {
	    return Boolean.TRUE.toString();
	}
	return Boolean.FALSE.toString();
    }
}
